package com.niit.plugin;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

// statement   executeQuery   executeUpdate
// prepareStatement
import java.util.Properties;

    @Intercepts({
            // type= Executor.class,method = "query" 确定拦截的“点”  Executor接口的 query方法
            // args query方法，全部参数的类型的数组
            @Signature(type= Executor.class,method = "query",
                    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
    })
public class TimePlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // TODO query执行之前，你的逻辑
        long startTime = System.currentTimeMillis();
        // invocation.getArgs()[0]; //获取查询的参数 SQL语句对象的参数

        Object result = invocation.proceed(); // 执行目标方法query，本来应该执行的代码
        // TODO query方法执行之后，你的逻辑
        long endTime = System.currentTimeMillis();
        System.out.println("查询耗时："+(endTime-startTime));
        return result;
    }

    @Override
    public Object plugin(Object target) {
        return Interceptor.super.plugin(target);
    }

    @Override
    public void setProperties(Properties properties) {
        Interceptor.super.setProperties(properties);
    }
}
